package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.ADDeleteBO;
import com.cskaoyan.bean.bo.ADParam;
import com.cskaoyan.bean.bo.BaseParam;
import com.cskaoyan.bean.pojo.MallAd;
import com.cskaoyan.bean.pojo.MallAdExample;

import com.cskaoyan.bean.vo.AdvertisementListVo;
import com.cskaoyan.mapper.MallAdMapper;
import com.cskaoyan.service.AdvertisementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author JJ
 * @description 广告服务实现类
 * @time 2021/7/8 22:15
 */
@Service
public class AdvertisementServiceImpl implements AdvertisementService {

    @Autowired
    MallAdMapper mallAdMapper;

    @Override
    public BaseData<AdvertisementListVo> listAdvertisement(ADParam param) {

        //执行查询之前写一行代码即可
        PageHelper.startPage(param.getPage(), param.getLimit());

        MallAdExample mallAdExample = new MallAdExample();

        //查询所有只设置一个排序和是否删除即可即可
        MallAdExample.Criteria criteria = mallAdExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (param.getName() != null) {
            criteria.andNameLike("%" +param.getName()+"%");
        }

        if (param.getContent() != null) {
            criteria.andContentLike("%" + param.getContent() + "%");
        }


        mallAdExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<MallAd> mallAds = mallAdMapper.selectByExample(mallAdExample);

        List<AdvertisementListVo> advertisementListVos = new ArrayList<>();
        for (MallAd mallAd : mallAds) {
            AdvertisementListVo advertisementListVo = new AdvertisementListVo(mallAd.getId(), mallAd.getName(), mallAd.getLink(), mallAd.getUrl(), mallAd.getPosition(), mallAd.getContent(), mallAd.getEnabled(), mallAd.getAddTime(), mallAd.getUpdateTime(), mallAd.getDeleted());
            advertisementListVos.add(advertisementListVo);
        }

        //total可以使用countByExample，也可以使用PageHelper来获得
        //int total = mallUserMapper.countByExample(mallUserExample);

        //在执行查询之后可以获得分页信息,将查询结果放入PageInfo的构造方法里
        PageInfo<AdvertisementListVo> pageInfo = new PageInfo<>(advertisementListVos);
        int total = (int) pageInfo.getTotal();

        return BaseData.ok(advertisementListVos, total);
    }


    @Override
    public Integer createAdvertisement(MallAd param) {
//        MallAdExample mallAdExample = new MallAdExample();

        Date date = new Date();
        param.setAddTime(date);
        param.setUpdateTime(date);
        int i = mallAdMapper.insertSelective(param);
        return i;

    }

    @Override
    public List<MallAd> queryAdvertisement(MallAd param) {
        MallAdExample mallAdExample = new MallAdExample();

        MallAdExample.Criteria criteria = mallAdExample.createCriteria();

        if (param.getId() != null) {
            criteria.andIdEqualTo(param.getId());
        }
        if (param.getName() != null) {
            criteria.andNameEqualTo(param.getName());
        }
        List<MallAd> mallAds = mallAdMapper.selectByExample(mallAdExample);
        return mallAds;
    }


    @Override
    public Integer updateAdvertisement(MallAd param) {
        MallAdExample mallAdExample = new MallAdExample();
        MallAdExample.Criteria criteria = mallAdExample.createCriteria();

        if (param.getId() != null) {
            criteria.andIdEqualTo(param.getId());
        }
        Date date = new Date();
        param.setUpdateTime(date);

        int i = mallAdMapper.updateByExampleSelective(param, mallAdExample);
        return i;
    }


    @Override
    public Integer deleteAD(ADDeleteBO param) {

        MallAdExample mallAdExample = new MallAdExample();

        //里面包含List<Criterion> 条件列表
        MallAdExample.Criteria criteria = mallAdExample.createCriteria();

        criteria.andIdEqualTo(param.getId());


        MallAd mallAd = new MallAd();

        mallAd.setId(param.getId());
        mallAd.setName(param.getName());
        mallAd.setLink(param.getLink());
        mallAd.setUrl(param.getUrl());
        mallAd.setPosition((byte) param.getPosition());
        mallAd.setContent(param.getContent());
//        mallAd.setStartTime();
//        mallAd.setEndTime();
        mallAd.setEnabled(param.isEnabled());
        mallAd.setAddTime(param.getAddTime());
        mallAd.setUpdateTime(param.getUpdateTime());
        mallAd.setDeleted(true);

        int i = mallAdMapper.updateByExampleSelective(mallAd, mallAdExample);

        return i;
    }


}
